package com.example.zhaojianxu.signin.signIn.main

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.support.v4.app.ActivityCompat
import android.support.v4.app.FragmentTransaction
import android.support.v4.content.ContextCompat
import android.view.View
import com.bumptech.glide.Glide
import com.example.zhaojianxu.signin.R
import com.example.zhaojianxu.signin.bean.BaseActivity
import com.example.zhaojianxu.signin.signIn.server.LocationService
import com.example.zhaojianxu.signin.signIn.sign.view.SignFragment
import com.example.zhaojianxu.signin.signIn.teacher.view.TeacherFragment
import com.example.zhaojianxu.signin.common.util.ToastUtil
import kotlinx.android.synthetic.main.activity_main_sign.*
import kotlinx.android.synthetic.main.common_title_bar.*

/**
 * 签到
 */
class SignMainActivity : BaseActivity(), View.OnClickListener {

    var mSignFragment: SignFragment? = null
    var mTeacherFragment: TeacherFragment? = null

    override fun onClick(v: View?) {
        hideFragment()
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        when (v?.id) {
            R.id.main_sign -> {
                common_title.text = "课堂"
                if (mSignFragment == null){
                    mSignFragment = SignFragment()
                    transaction.add(R.id.main_frame,mSignFragment!!)
                }else{
                    transaction.show(mSignFragment!!)
                }
                transaction.commit()
            }
            R.id.main_teacher -> {
                common_title.text = "教师"
                if (mTeacherFragment == null){
                    mTeacherFragment = TeacherFragment()
                    transaction.add(R.id.main_frame,mTeacherFragment!!)
                }else{
                    transaction.show(mTeacherFragment!!)
                }
                transaction.commit()
            }
        }
    }

    private fun hideFragment() {
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        if (mSignFragment != null){
            transaction.hide(mSignFragment!!)
        }
        if (mTeacherFragment != null){
            transaction.hide(mTeacherFragment!!)
        }
        transaction.commit()
    }

    override fun initView() {
        common_back.visibility = View.GONE
        common_setting.visibility = View.GONE
        main_sign.setOnClickListener(this)
        main_teacher.setOnClickListener(this)
    }

    override fun initData() {
        val transaction: FragmentTransaction = supportFragmentManager.beginTransaction()

        if (mSignFragment == null){
            mSignFragment = SignFragment()
            transaction.add(R.id.main_frame,mSignFragment!!)
            common_title.text = "课堂"
        }

        transaction.commit()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main_sign)

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            startService()
        } else {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 0)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            0 -> {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED && permissions[0] == Manifest.permission.ACCESS_FINE_LOCATION) {
                    startService()
                } else {
                    ToastUtil.instance.showToast(this, "权限开启失败！")
                }
            }
        }
    }

    private fun startService(){
        val intent = Intent(this,LocationService::class.java)
        startService(intent)
    }
}
